package com.wswy.wzcx.ui.adapter;


import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.ArraySet;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.blankj.utilcode.util.SizeUtils;
import com.facebook.common.util.UriUtil;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.dfnew.News;
import com.wswy.wzcx.ui.hold.ViewHolder;
import com.wswy.wzcx.ui.main.home.LazyLoadListener;
import com.wswy.wzcx.ui.web.WebViewActivity;
import java.util.HashMap;
import java.util.List;

/**
 * Created by wangduheng26 on 2018/2/27.
 * wangduheng26@gmail.com
 */
@Deprecated
public class NewsAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "NewsAdapter2";

    private static final int TYPE_NORMAL = 0;
    private static final int TYPE_AD_01 = 1;

    private List<Object> list;
    private LazyLoadListener loadListener;
    private ArraySet<String> readMap = new ArraySet<>(100);
    private HashMap<NativeExpressADView, Integer> mAdViewPositionMap;

    public NewsAdapter2(List<Object> list, HashMap<NativeExpressADView, Integer> mAdViewPositionMap) {
        this.list = list;
        this.mAdViewPositionMap=mAdViewPositionMap;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        if (viewType == TYPE_NORMAL) {
            return new NewsVH(inflater.inflate(R.layout.news_item, parent, false));

        } else if (viewType == TYPE_AD_01) {
            return new AD01VH(inflater.inflate(R.layout.ad01_item, parent, false));
        } else {
            return null;
        }

    }

    // 把返回的NativeExpressADView添加到数据集里面去
    public void addADViewToPosition(int position, NativeExpressADView adView) {
        if (position >= 0 && position < list.size() && adView != null) {
            list.add(position, adView);
        }
    }

    // 移除NativeExpressADView的时候是一条一条移除的
    public void removeADView(int position, NativeExpressADView adView) {
        list.remove(position);
        notifyItemRemoved(position); // position为adView在当前列表中的位置
        notifyItemRangeChanged(0, list.size() - 1);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Object item = getItem(position);
        if (holder instanceof NewsVH && item instanceof News) {
            if (position == getItemCount() - 1) {
                lazyLoad(((News) item).rowkey);
            }
            ((NewsVH) holder).bind((News) item);
        }else if(holder instanceof AD01VH){
            AD01VH customViewHolder= (AD01VH) holder;
            final NativeExpressADView adView = (NativeExpressADView) list.get(position);
            mAdViewPositionMap.put(adView, position); // 广告在列表中的位置是可以被更新的
            if (customViewHolder.container.getChildCount() > 0
                    && customViewHolder.container.getChildAt(0) == adView) {
                return;
            }


            if (customViewHolder.container.getChildCount() > 0) {
                customViewHolder.container.removeAllViews();
            }

            if (adView.getParent() != null) {

                ((ViewGroup) adView.getParent()).removeView(adView);
            }


            customViewHolder.container.addView(adView);
            adView.render(); // 调用render方法后sdk才会开始展示广告
        }

    }

    public void setLoadListener(LazyLoadListener loadListener) {
        this.loadListener = loadListener;
    }

    private void lazyLoad(String rowkey) {
        if (loadListener != null
                && !loadListener.isLoading()
                && !loadListener.isEnd()) {
            loadListener.lazyLoad(rowkey);
        }
    }

    @Override
    public int getItemViewType(int position) {
        Object item = getItem(position);
        if (item instanceof News) {
            return TYPE_NORMAL;
        } else if (item instanceof NativeExpressADView) {
            return TYPE_AD_01;
        } else {
            return super.getItemViewType(position);
        }
    }

    private Object getItem(int position) {
        if (list != null && !list.isEmpty()) {
            int size = list.size();
            if (position < size) {
                return list.get(position);
            }
        }
        return null;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    public class NewsVH extends ViewHolder implements View.OnClickListener {
        public ImageView sdv;
        public TextView tvTitle;
        public TextView tvContent;
        private News mItem;

        private NewsVH(View itemView) {
            super(itemView);
            sdv = itemView.findViewById(R.id.sdv);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvContent = itemView.findViewById(R.id.tv_desc);
            itemView.setOnClickListener(this);

        }

        public void bind(News item) {
            mItem = item;
            if (item != null) {
                tvTitle.setText(item.topic);
                tvContent.setText(TextUtils.concat(item.source, "  · ", item.date));
                List<News.ImgWare> miniimg = item.lbimg;
                if (miniimg != null && !miniimg.isEmpty()) {
                    sdv.setImageURI(UriUtil.parseUriOrNull(miniimg.get(0).src));
                }
                if (readMap.contains(item.pk)) {
                    tvTitle.setTextColor(ContextCompat.getColor(tvTitle.getContext(), R.color.gray60));
                } else {
                    tvTitle.setTextColor(ContextCompat.getColor(tvTitle.getContext(), R.color.gray20));
                }

            }

        }

        @Override
        public void onClick(View v) {
            if (mItem != null) {
                Context context = v.getContext();
                WebViewActivity.startWebview(context, "热门资讯", mItem.url, true,false,true,false);
                readMap.add(mItem.pk);
                notifyItemChanged(getAdapterPosition());
            }

        }
    }
    public class AD01VH extends ViewHolder{
//        public TextView title;
        public ViewGroup container;
        private AD01VH(View view) {
            super(view);
//            title = (TextView) view.findViewById(R.id.title);
            container = (ViewGroup) view.findViewById(R.id.express_ad_container);
        }
    }
}
